package org.abettor.myinj.ui;

import org.abettor.myinj.ui.tabs.CheckInjectionPoint;
import org.abettor.myinj.util.I18n;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;

/**
 * 主窗口对话框类
 * @author abettor
 *
 */
public class MainDialog
{
	private static I18n i18n = new I18n();

	private final static String ICON = "./img/logo.png";
	private final static int WIDTH = 1024;
	private final static int HEIGHT = 768;
	private final static String TITLE = i18n.getString("main.title");

	private Display display = Display.getDefault();
	private Shell shell;
	private TabFolder tabFolder;
	private TabItem tabItemCheck;
	private CheckInjectionPoint compositeCheck;
	private TabItem tabItemStatement;
	private TabItem tabItemStructure;
	private TabItem tabItemData;
	private TabItem tabItemConfig;

	public void show()
	{
		Monitor monitor = display.getPrimaryMonitor();
		Rectangle rect = monitor.getBounds();

		//	创建对话框Shell对象
		shell = new Shell(display, SWT.DIALOG_TRIM | SWT.MIN | SWT.SYSTEM_MODAL);
		shell.setText(TITLE);
		shell.setSize(WIDTH, HEIGHT);
		shell.setLocation((rect.width - WIDTH) / 2, (rect.height - HEIGHT) / 2);

		//	设置图标
		Image logo = new Image(display, ICON);
		shell.setImage(logo);
		logo.dispose();

		//	添加主要功能选项卡
		Rectangle clientRect = shell.getClientArea();
		tabFolder = new TabFolder(shell, SWT.TOP | SWT.BORDER);
		tabFolder.setBounds(0, 0, clientRect.width, clientRect.height);
		tabItemCheck = new TabItem(tabFolder, SWT.NONE);
		tabItemCheck.setText(i18n.getString("main.tabs.check.name"));
		compositeCheck = new CheckInjectionPoint(tabFolder, SWT.NONE);
		tabItemCheck.setControl(compositeCheck);
		compositeCheck.show();

		tabItemStatement = new TabItem(tabFolder, SWT.NONE);
		tabItemStatement.setText(i18n.getString("main.tabs.statement.name"));

		tabItemStructure = new TabItem(tabFolder, SWT.NONE);
		tabItemStructure.setText(i18n.getString("main.tabs.structure.name"));

		tabItemData = new TabItem(tabFolder, SWT.NONE);
		tabItemData.setText(i18n.getString("main.tabs.data.name"));

		tabItemConfig = new TabItem(tabFolder, SWT.NONE);
		tabItemConfig.setText(i18n.getString("main.tabs.config.name"));

		//	开始窗口主循环
		shell.open();
		while (! shell.isDisposed())
		{
			if (! display.readAndDispatch())
			{
				display.sleep();
			}
		}

		shell.dispose();
	}
}
